# # 财务
# # 添加应收款13
# 全部删除，点击取消
from selenium import webdriver # 调用webdriver
from selenium.webdriver.common.by import By # 引入by模块
from time import sleep # 调用sleep
driver = webdriver.Chrome() #打开浏览器
try:
    driver.maximize_window()  # 最大化浏览器
    driver.get("http://192.168.1.5/crm") # 访问crm后台
    # cookie={"name":"PHPSESSID",'value': 'pj3roa0363o21h9vvv8c296bi0'}
    # driver.add_cookie(cookie)
    # driver.get("http://192.168.1.79/crm/index.php?m=finance&a=add&t=receivables")  # 访问crm添加产品页面
    #定位器
    #登录
    name = "admin"
    password = "123456"  # 要去按F12验证
    name_locator = (By.NAME, 'name') #用户名
    password_locator = (By.NAME, 'password') #密码
    login_locator = (By.NAME, 'submit')#登录按钮
    # #商机
    Business_locator = (By.XPATH, "/html/body/div[1]/div/div/div[2]/ul[1]/li[3]/a") #商机
    add_Business_locator = (By.XPATH,'/html/body/div[5]/div[2]/div[1]/div/a') #添加商机
    client_locator = (By.ID,'customer_name')#客户
    chose_client_locator = (By.NAME,'customer')#选择客户
    affirm_locator = (By.XPATH, '/html/body/div[10]/div[3]/div/button[1]/span')#确认按钮
    Business_name_locator = (By.ID,'name')#商机名
    plan_locator = (By.ID, 'estimate_price') #预计成交价
    save_sj_locator = (By.XPATH,'/html/body/div[5]/div[2]/div/form/table/tfoot/tr/td/input[1]') #保存
    #
    # # #合同
    contract_s_locator = (By.LINK_TEXT, '合同') #合同
    add_contract_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/a')#添加合同
    business_name_locator = (By.ID, 'business_name')#来源商机
    business_name_click_locator = (By.XPATH, '/html/body/div[7]/div[2]/table/tbody[2]/tr/td[1]/input[1]')#选择商机
    business_confirm_locator = (By.XPATH, '/html/body/div[7]/div[3]/div/button[1]/span')#ok按钮
    price_locator=(By.ID,"price") #合同金额
    contract_save_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/form/table/tfoot/tr/td[2]/input[1]')#保存
    #
    # #客户
    customers_locator = (By.XPATH, '/html/body/div[1]/div/div/div[2]/ul[1]/li[2]/a')#客户
    xj_customers_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/a')#新建客户
    customers_name_locator = (By.ID, 'name')#客户名称
    con_name_locator = (By.NAME, 'con_name')#联系人名称
    con_telephone_locator = (By.NAME, 'con_telephone')#联系人号码
    bc_locator = (By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[1]')#
    #
    #
    # 财务
    finance_locator = (By.LINK_TEXT,"财务" )  # 定位财务
    btn_locator=(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[3]/a[1]') #添加应收款
    ys_name_locator=(By.XPATH,"/html/body/div[5]/div[2]/div/form/table/tbody/tr[2]/td[2]/input")#应收款名
    contract_name_locator=(By.ID,"contract_name")#合同
    contract_locator=(By.CSS_SELECTOR,"#data > tr > td:nth-child(1) > input[type=radio]")
    ui_locator=(By.XPATH,"/html/body/div[8]/div[3]/div/button[1]") #合同ok按钮
    pay_time_locator=(By.ID,"pay_time")#收款时间
    description_locator=(By.XPATH,"/html/body/div[5]/div[2]/div/form/table/tbody/tr[9]/td[2]/textarea")#收款时间
    sk_submit_locator=(By.NAME,"submit")
    check_add_order_locator=(By.ID,"check_add_order")#点击同时创建收款单
    delete_ys_locator = (By.ID, "check_all")  # 定位选择删除的应收
    delete_locator = (By.ID, "delete")  # 删除
    #


    # #查找应收元素
    #登录
    driver.find_element(*name_locator).send_keys(name) #输入用户名
    sleep(1)
    driver.find_element(*password_locator).send_keys(password) #输入密码
    sleep(2)
    driver.find_element(*login_locator).click()  #点击按钮

    # # #商机
    driver.find_element(*Business_locator).click()  # 点击商机
    driver.find_element(* add_Business_locator).click()  # 点击添加商机
    driver.find_element(* Business_name_locator).send_keys('EXo')  # 输入商机名
    sleep(2)
    driver.find_element(*client_locator).click()  # 点击客户
    sleep(3)
    driver.find_element(*chose_client_locator).click()  # 选择客户
    driver.find_element(*affirm_locator).click()  # 点击确定
    driver.find_element(*plan_locator ).send_keys('005')  # 输入预计成交价
    sleep(2)
    driver.find_element(* save_sj_locator).click()  # 点击保存
    sleep(2)
    # # 合同
    driver.find_element(*contract_s_locator).click() #查找合同元素，并点击
    sleep(2)
    driver.find_element(*add_contract_locator).click()#查找添加合同元素，并点击
    driver.find_element(*business_name_locator).click()#查找来源商机，并点击
    sleep(5)
    driver.find_element(*business_name_click_locator).click()#查找商机元素，并点击
    sleep(3)
    driver.find_element(*business_confirm_locator).click()#查找ok元素，并点击
    driver.find_element(*price_locator).send_keys("10000") #查找合同金额元素
    driver.find_element(*contract_save_locator).click()#点击保存
    sleep(2)
    # #
    # # #客户
    driver.find_element(*customers_locator).click()  # 定位到客户界面
    driver.find_element(*xj_customers_locator).click()# 点击新建客户
    driver.find_element(*customers_name_locator).send_keys('拉了')  # 输入客户名称
    driver.find_element(*con_name_locator).send_keys('徐徐')# 输入联系人姓名
    driver.find_element(*con_telephone_locator).send_keys('18797962555') # 输入联系人手机号
    driver.find_element(*bc_locator).click()# 点击保存
    sleep(2)

    #财务
    finance_locator=driver.find_element(*finance_locator).click()#点击财务
    btn_locator=driver.find_element(*btn_locator).click()#点击添加应收款
    ys_name_element = driver.find_element(*ys_name_locator).send_keys("划话") #查找应收款名元素，并输入
    sleep(2)
    contract_name_element=driver.find_element(*contract_name_locator).click()#点击合同
    sleep(2)
    contract_element=driver.find_element(*contract_locator).click()#选择合同
    sleep(2)
    ui_element=driver.find_element(*ui_locator).click()#点击ok键
    sleep(2)
    # driver.find_element(*check_add_order_locator).click()#点击同时创建收款单
    pay_time_element=driver.find_element(*pay_time_locator) #查找收款时间元素
    pay_time_element.clear()#清理
    pay_time_element.send_keys("2022-09-22") #输入收款时间
    sleep(2)
    driver.find_element(*description_locator).send_keys("项目最终决定按原来计划实施(……200字)")#输入描述内容
    sleep(2)
    driver.find_element(*sk_submit_locator).click()  #点击保存
    sleep(2)
    driver.find_element(*delete_ys_locator).click()  # 查找应收款冷吃兔选中
    sleep(2)
    driver.find_element(*delete_locator).click()  # 查找删除按钮元素，并点击
    sleep(2)
    driver.switch_to.alert.dismiss()#取消
    sleep(2)
except:
    raise
finally:
    sleep(5) # 暂停五秒，并关闭浏览器
    driver.quit()